package cn.lanqiao.book.controller;

import cn.lanqiao.book.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/*
* 用于验证用户名是否重复（ajax请求）
* 请求中应包含 （String）username
* 响应中返回 一个Json字符串 {"msg","Repeat"}或{"msg","NoRepeat"}
* */
@WebServlet("/isRepeat")
public class IsRepeatUser extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        String username=req.getParameter("username");
        if(!username.equals("")) {
            Map<String, String> map = new HashMap<>();
            if (UserServiceImpl.getInstance().existUsername(username)) {
                map.put("msg", "Repeat");
            } else {
                map.put("msg", "NoRepeat");
            }
            String Json = JSON.toJSONString(map);
            resp.getWriter().write(Json);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        doGet(req,resp);
    }
}
